package com.zyd.blog.controller;

import com.github.pagehelper.PageInfo;
import com.zyd.blog.business.entity.ArticleFavor;
import com.zyd.blog.business.entity.User;
import com.zyd.blog.business.service.BizArticleFavorService;
import com.zyd.blog.business.vo.ArticleFavorConditionVO;
import com.zyd.blog.framework.object.PageResult;
import com.zyd.blog.framework.object.ResponseVO;
import com.zyd.blog.util.ResultUtil;
import com.zyd.blog.util.SessionUtil;
import com.zyd.blog.util.SnowflakeIdWorker;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 收藏
 *
 * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
 * @version 1.0
 * @website https://www.zhyd.me
 * @date 2018/4/24 14:37
 * @since 1.0
 */
@Slf4j
@RestController
@RequestMapping("/articleFavor")
public class RestArticleFavorController {
    @Autowired
    private BizArticleFavorService articleFavorService;

    @RequiresPermissions("articles")
    @PostMapping("/add/{id}")
    public ResponseVO add(@PathVariable("id") Long id) {
        ArticleFavor articleFavor = new ArticleFavor();
        User loginUser = SessionUtil.getUser();
        if(loginUser != null) {
            if(loginUser.getId() != null) {
                articleFavor.setUserId(loginUser.getId());
                articleFavor.setArticleId(id);
                articleFavor.setId(SnowflakeIdWorker.getWorker().nextId());
                ArticleFavor af = articleFavorService.insert(articleFavor);
                return ResultUtil.success(af);
            }
        }
        return ResultUtil.error("操作失败");
    }

    @RequiresPermissions("articles")
    @PostMapping("/delete/{id}")
    public ResponseVO delete(@PathVariable("id") Long id) {
        ArticleFavor articleFavor = new ArticleFavor();
        User loginUser = SessionUtil.getUser();
        if(loginUser != null) {
            if(loginUser.getId() != null) {
                articleFavor.setUserId(loginUser.getId());
                articleFavor.setArticleId(id);
                ArticleFavor af = articleFavorService.delete(articleFavor);
                return ResultUtil.success(af);
            }
        }
        return ResultUtil.error("操作失败");
    }

    @PostMapping("/list")
    public PageResult list() {

        User loginUser = SessionUtil.getUser();
        if(loginUser != null) {
            if(loginUser.getId() != null) {
                ArticleFavorConditionVO articleFavorConditionVO = new ArticleFavorConditionVO();
                articleFavorConditionVO.setUserId(loginUser.getId());
                PageInfo<ArticleFavor> pageInfo = articleFavorService.findPageBreakByCondition(articleFavorConditionVO);
                return ResultUtil.tablePage(pageInfo);
            }
        }
        return ResultUtil.tablePage(null);
    }

    //获取谋篇文章被哪些人收藏了（涉及隐私）
    @PostMapping("/list/{id}")
    public PageResult list(@PathVariable("id") Long id) {
        User loginUser = SessionUtil.getUser();
        if(loginUser != null) {
            if(loginUser.getId() != null) {
                ArticleFavorConditionVO articleFavorConditionVO = new ArticleFavorConditionVO();
                articleFavorConditionVO.setArticleId(id);
                PageInfo<ArticleFavor> pageInfo = articleFavorService.findPageBreakByCondition(articleFavorConditionVO);
                return ResultUtil.tablePage(pageInfo);
            }
        }
        return ResultUtil.tablePage(null);
    }

    @PostMapping("/getId/{articleId}")
    public ResponseVO getId(@PathVariable("articleId") Long articleId) {
        User loginUser = SessionUtil.getUser();
        if(articleId != null) {
            if (loginUser != null) {
                if (loginUser.getId() != null) {
                    ArticleFavorConditionVO articleFavorConditionVO = new ArticleFavorConditionVO();
                    articleFavorConditionVO.setArticleId(articleId);
                    articleFavorConditionVO.setUserId(loginUser.getId());
                    Long id = articleFavorService.findFavorId(articleFavorConditionVO);
                    return ResultUtil.success(null, id);
                }
            }
        }
        return ResultUtil.error("操作失败");
    }
}
